<template>
  <div>
    <el-form :model="formData">
      <el-form-item label="菜单组件" label-width="100px">
        <el-input v-model="formData.assembly"></el-input>
      </el-form-item>

      <el-form-item label="父id" label-width="100px" v-show="id">
        <el-input v-model="formData.parentid" disabled></el-input>
      </el-form-item>

      <el-form-item label="菜单名称" label-width="100px">
        <el-input v-model="formData.menusname"></el-input>
      </el-form-item>

      <el-form-item label="菜单等级" label-width="100px" v-show="id">
        <el-input v-model="formData.level"></el-input>
      </el-form-item>

      <el-form-item label="菜单图标" label-width="100px">
        <el-input v-model="formData.icon"></el-input>
      </el-form-item>
      <el-form-item label="菜单路径" label-width="100px">
        <el-input v-model="formData.path"></el-input>
      </el-form-item>
      <el-form-item label="排序字段" label-width="100px">
        <el-input v-model="formData.sort"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  name: 'AddMenu',
  props: ['id'],
  data() {
    return {
      formData: {
        assembly: 1,
        menusname: 2,
        icon: 3,
        path: 4,
        sort: 5,
      }
    }
  },
  mounted () {
    this.$bus.$on('toAddMenus',this.toAddMenus);
    if(this.id){
      this.getMenusById();
    }
  },
  beforeDestroy(){
    this.$bus.$off('toAddMenus');
  },
  methods: {
    async getMenusById(){
      let res = await this.$API.SystemMenus.reqMenusById(this.id);
      if (res.code == 200) {
        this.formData = { ...res.data }
      }
    },
    async toAddMenus() {
      if (this.id) {
        let { assembly,parentid,menusname,level,icon,path,sort } = this.formData;
        let res = await this.$API.SystemMenus.reqUpdateMenus(this.id,assembly,parentid,menusname,level,icon,path,sort);
        if (res.code == 1) {
          this.$message({ message: '修改成功', type: 'success' });
        }
      } else {
        let { assembly, menusname, icon, path,sort } = this.formData;
        let query = this.$QS.stringify({ assembly, menusname, icon, path,sort });
        let res = await this.$API.SystemMenus.reqAddMenus(query);
        if (res.code == 1) {
          this.$message({ message: '添加成功', type: 'success' });
        }
      }
    },
  }
};
</script>

<style scoped></style>
